package udp_client;

import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import com.shaolei.basic.BASIC;

public class mySplit implements Runnable{
	private static BlockingQueue<byte[]> imageQ= new LinkedBlockingQueue<byte[]>();
	private static int IMAGESIZE= BASIC.get_IMAGESIZE();
	private static int SETSIZE= BASIC.get_SETSIZE();
	public void run(){
		int imageId=0;
		while(true){
			while (imageQ.isEmpty()==false){
				byte[] allArray= imageQ.peek();
				imageQ.remove();
				int groups= IMAGESIZE/SETSIZE;
				byte[] myID=BASIC.toByteArray(imageId);//only one image
				int index=0;
				for (int i=0; i<groups; i++){
					byte[] myStart=BASIC.toByteArray(index);
					byte[] myBody= Arrays.copyOfRange(allArray,index, index+ SETSIZE);
					byte[] mytemp= BASIC.combine(myID,myStart);//size 8
					int bodySize= myBody.length;
					byte[] mySize= BASIC.toByteArray(bodySize);
					byte[] myHead= BASIC.combine(mytemp, mySize);
					byte[] SingePkg=BASIC.combine(myHead, myBody);
					new mySend().get_pkgQ().add(SingePkg);
					index+=SETSIZE;
				}
				//the left contents
				if (index<IMAGESIZE){
					byte[] myStart=BASIC.toByteArray(index);
					byte[] myBody= Arrays.copyOfRange(allArray, index, IMAGESIZE);
					byte[] mytemp= BASIC.combine(myID, myStart);//size 8
					int bodySize= myBody.length;
					byte[] mySize= BASIC.toByteArray(bodySize);
					byte[] myHead= BASIC.combine(mytemp, mySize);
					byte[] SingePkg=BASIC.combine(myHead, myBody);
//					System.out.println("mySplit::"+fullSet.length+Arrays.toString(fullSet));
					new mySend().get_pkgQ().add(SingePkg);
				}
//				System.out.println("mySplit::"+ imageId+" "+new mySend().get_pkgQ().size());
				imageId++;
			}
		}
	}
	
	public Queue<byte[]> get_imageQ(){
		return imageQ;
	}
}
